package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

//<el-upload action="/ordersetting/upload.do"
//                                       name="excelFile"
//                                       :show-file-list="false"
//                                       :on-success="handleSuccess"
//                                       :before-upload="beforeUpload">
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    //上传文件
    @Reference
    private OrderSettingService orderSettingService;
    @PostMapping("/upload.do")
    public Result upload(@RequestParam("excelFile")MultipartFile multipartFile) {
        List<String[]> list =null;
        try {
            list = POIUtils.readExcel(multipartFile);

            } catch (IOException e) {
                e.printStackTrace();
        }
        return this.orderSettingService.batchAdd(list);
    }
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(@RequestParam("month")String month){   //yyyy-MM
        Result result = this.orderSettingService.getOrderSettingByMonth(month);
        return result;
    }
    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try{
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
